//
//  CRKeyboardView.m
//  CRChatRoom
//
//  Created by lanou3g on 15/11/20.
//  Copyright © 2015年 SecondGroup. All rights reserved.
//

#import "CRKeyboardView.h"

@interface CRKeyboardView ()<UITextFieldDelegate>
@property (nonatomic, strong) UIImageView *backImageView;
@end


@implementation CRKeyboardView
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super  initWithFrame:frame]) {
        [self initControlWithKeyboard];
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

/**
 *  键盘控件初始化
 */
- (void)initControlWithKeyboard
{
    //背景图
    self.backImageView = [[UIImageView alloc] initWithFrame:self.bounds];
    UIImage *image = [UIImage imageNamed:@"toolbar_bottom_bg"];
    self.backImageView.image = [image stretchableImageWithLeftCapWidth:image.size.width * 0.5 topCapHeight:image.size.height * 0.5];
    
    //初始化声音按键
    self.voiceBtn  = [self buttonNormalWith:@"iconfont-yuyin-2" hightLight:nil action:nil];
    [self.voiceBtn setFrame:CGRectMake(0, 0, 33, 33)];
    [self.voiceBtn setCenter:CGPointMake(25, self.height * 0.5)];
    [self addSubview:self.voiceBtn];
    
    //添加（+）按钮
    self.addBtn = [self buttonNormalWith:@"iconfont-font45-2" hightLight:@"iconfont-font45" action:@selector(clickAddBtnAction:)];
    [self.addBtn setFrame:CGRectMake(0, 0, 33, 33)];
    [self.addBtn setCenter:CGPointMake(self.width - 65, self.height * 0.5)];
    [self addSubview:self.addBtn];
    
    //初始化录音按钮
    self.speakBtn = [self buttonNormalWith:nil hightLight:nil action:@selector(speakBtnPress:)];
    [self.speakBtn setTitle:@"按住说话🔊" forState:UIControlStateNormal];
    [self.speakBtn setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted];
    [self.speakBtn addTarget:self action:@selector(touchDown:) forControlEvents:UIControlEventTouchDown];
    [self.speakBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.speakBtn setBackgroundColor:[UIColor whiteColor]];
    self.speakBtn.hidden = YES;
    [self addSubview:self.speakBtn];
    
    //初始化相机
    self.cemareBtn = [self buttonNormalWith:@"iconfont-xiangji" hightLight:nil action:@selector(clickCemareButtonAction:)];
    [self.cemareBtn setFrame:CGRectMake(0, 0, 33, 33)];
    [self.cemareBtn setCenter:CGPointMake(self.width - 25, self.height * 0.5)];
    [self addSubview:self.cemareBtn];
    
    //初始化输入框
    self.textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 2, self.width - 60 - 80, 35)];
    self.textField.returnKeyType = UIReturnKeySend;
    self.textField.center = CGPointMake(self.width / 2 - 20, self.height * 0.5);
    self.textField.font = [UIFont fontWithName:@"HelveticaNeue" size:14];
    self.textField.placeholder = @"请输入";
    self.textField.textAlignment = NSTextAlignmentCenter;
    self.textField.leftViewMode = UITextFieldViewModeAlways;
    [self.textField setBackground:[UIImage imageNamed:@"chat_bottom_textBack"]];
    [self.speakBtn setFrame:self.textField.frame];
    
    //设置代理
    self.textField.delegate = self;
    [self addSubview:self.textField];
    
    
    
}
- (void)clickCemareButtonAction:(UIButton *)button
{
    
}
/**
 *  按住spaek按钮开始录音
 *
 */
- (void)touchDown:(UIButton *)speak
{
    if ([self.keyboardDelegate respondsToSelector:@selector(beginRecord)]) {
        [self.keyboardDelegate beginRecord];
    }
    NSLog(@"***********开始录音");
}
/**
 *  录音按钮结束录音
 *
*/
- (void)speakBtnPress:(UIButton *)speak
{
    if ([self.keyboardDelegate respondsToSelector:@selector(finishRecord)]) {
        [self.keyboardDelegate finishRecord];
    }
    NSLog(@"结束录音");
}
/**
 *  按添加功能按钮
 *
*/
- (void)clickAddBtnAction:(UIButton *)addBtn
{
    
}
/**
 *  封装Button初始化属性
 *
 *  @param normal     常态照片
 *  @param hightLight 高亮状态下照片
 *  @param action     Button事件
 *
 *  @return 将封装的Button返回
 */
- (UIButton *)buttonNormalWith:(NSString *)normal hightLight:(NSString *)hightLight action:(SEL)action
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:[UIImage imageNamed:normal] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:hightLight] forState:UIControlStateHighlighted];
    [btn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    return btn;
}









@end
